/*
 * IpcButton.java
 * 
 * Created on Apr 9, 2009, 2:55:30 AM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.ampl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Vector;
import javax.swing.JButton;
import portfolio.mainScreen;

/**
 *
 * @author ACA
 */
public class IpcButton extends JButton implements ActionListener 
{
    private mainScreen  mainFrame;
    private Connection  connection;
    
    public IpcButton (mainScreen mainFrameParam)
    {
        super ("Ipc");

        mainFrame = mainFrameParam;
        connection = null;          //<<<< new 09/2009
        this.addActionListener(this);

        return;
    }
    
    public IpcButton (mainScreen mainFrameParam, Connection connectionParam)
    {
        super ("Ipc");

        mainFrame = mainFrameParam;
        connection = connectionParam;
        this.addActionListener(this);

        return;
    }
    /***********************************************************/
    public void actionPerformed(ActionEvent e) {
        String sAcctionCommand = e.getActionCommand ();
        System.out.println (sAcctionCommand);
        Vector vIPCPrices = null; //new Vector ();

        try {
            //double [] dIPCReturns = portfolioDB.createIpcTable (sAcctionCommand, vTermTimeLine);
            //double dReturn     = Miscellanea.getReturn(dIPCReturns,  dIPCReturns.length);
            //double dVariance   = Miscellanea.getVar (dIPCReturns);
            //Double DRisk       = new Double (Math.sqrt(dVariance));
            //Double DReturn     = new Double (dReturn);

            mainFrame.displayMessage ("--", "\n----------------------------------------------\n");
            mainFrame.displayMessage ("--", "IPC\n");
            mainFrame.displayMessage ("--", "----------------------------------------------\n");
            //mainFrame.displayMessage ("--", "Risk:\t" + DRisk.toString() + "\t");
            //mainFrame.displayMessage ("--", "Return:\t" + DReturn.toString() + "\n");

        } catch (Exception exp) {
            mainFrame.displayMessage ("--", exp.toString() + "\n");
            //JOptionPane.showMessageDialog (null, exp);                    
        }

    } /* End Method actionPerformed */
    ////////////////////////////////////////////////////////////
    public double [] createIpcTable (String sAcctionCommand,
                                     Vector vTermTimeLine) throws Exception
    {
//        if (vTermTimeLine == null) {
//            throw new Exception ("portfolioDB::createIpcTable: null vTermTimeLine.");
//        }
//        double [] dOutReturn = null;
////        System.out.println (sAcctionCommand);
//        Vector vStocksReturn = new Vector ();
//
//        try {
//            Statement statement = connection.createStatement ();
//            //statement.execute ("USE portfolio;");
//            Vector vTermLine2 = this.getStockPrice("BMVStocks", "macroIndexes", "tradeDate", vTermTimeLine); // (o)j(-)
//            MiscellaneaDB.createTradeDateColumn("portfolio", "ipcClose", vTermLine2);
//            this.createStockColumn ("IPCclose", "IPCclose");
//            
//            Vector vPrice     = this.getStockPrice("BMVStocks", "macroIndexes", "IPCclose", vTermLine2);
//            
//            int iRows = vPrice.size();
//            if (iRows < 2) {
//                throw new Exception ("portfolioDB::createIpcTable: not enoughrows on vPrice.");
//            }
//
//            dOutReturn = new double [iRows];
//            
////            System.out.println (vTermLine2.get(0).toString());
//            statement.execute ("UPDATE portfolio.IPCclose SET " +
//                               "IPCclose" + "= 0 " +
//                               "WHERE tradeDate = '" + vTermLine2.get(0).toString () +"';");
//            dOutReturn [0] = 0;
//            for (int i = 1; i < iRows; i++) {
//                Double DPrice0 = new Double (vPrice.get (i -1).toString()); //DInputData [i-1].doubleValue();
//                Double DPrice1 = new Double (vPrice.get (i   ).toString()); //DInputData   [i].doubleValue();
//                double dPrice0 = DPrice0.doubleValue();
//                double dPrice1 = DPrice1.doubleValue();
//                double dReturn = (dPrice0 != 0)? 
//                                  ((dPrice1 - dPrice0)/dPrice0):0;
//                //System.out.println (dReturn);                  
//                statement.execute ("UPDATE portfolio.IPCclose SET " +
//                                   "IPCclose" + "=" + 
//                                   dReturn + 
//                                   " " + "WHERE tradeDate='" + vTermLine2.get(i).toString () +"';");
//                dOutReturn [i -1] = dReturn;
//                //dSumReturn += dReturn;
//                    
//            }                        
//            //////////////////////////
//        } catch (SQLException exp) {
//            new Exception (exp);                   
//        } 
//        
          return null; //dOutReturn;
    } /* End createIpcMethod */    
    ////////////////////////////////////////////////////////////
    /**********************************************************/
//    public Vector getStockPrice (String sDBName, String sTable, String sColumnName, Vector vTradeDateLine)  throws Exception
//    {
//        if (vTradeDateLine.isEmpty()) {
//            throw new Exception ("portfolioDB::getStockPrice: ERROR: empty vTradeDateLine.");
//        }
//        int iTradeDateLen    = vTradeDateLine.size();
//        double [] dPriceVect = new double [iTradeDateLen];
//        Statement statement = null;
//
//        String sBeginDate = vTradeDateLine.get (0).toString ();
//        String sEndDate   = vTradeDateLine.lastElement().toString();
//        try {
//            statement = connection.createStatement ();
//            statement.execute ("use " + sDBName + ";");
//        }
//        catch (SQLException sqlex) {
//            throw new Exception (sqlex);
//        }
//        
//        Vector vOutVector = new Vector ();
//        for (int i = 0; i < iTradeDateLen; i++) {
//            try {
//                ResultSet resultSet = statement.executeQuery ("SELECT " + sColumnName + " FROM " + sTable + " WHERE '" +
//                                                    vTradeDateLine.get (i).toString() + "' = tradeDate;");
//                ResultSetMetaData rsmd = resultSet.getMetaData();
//                resultSet.first ();
//                if (rsmd.getColumnType(1) == java.sql.Types.DATE) {
//                    vOutVector.add (resultSet.getDate ("tradeDate").toString());
//                    //System.out.println   (vOutVector.get(i -1).toString());
//                }
//                else {
//                    Double DTmp = new Double (resultSet.getDouble (sColumnName));
//                    vOutVector.add (DTmp); //new Double (resultSet.getDouble (sColumnName)));
//                    //System.out.println (DTmp.toString());//vOutVector.get(i -1).toString());
//                }
//            } catch (SQLException e) {
//                //System.err.println(e);
//            }
//
//        }        
//        return vOutVector;
//    } /* End Method getStockPrice */
} /* End class IpcButton */    
